//
//  ByBlueToothDataOperationModel.m
//  laserdevice_ios
//
//  Created by pro on 16/12/10.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByBlueToothDataOperationModel.h"

// 数据测量model
#import "ByBlueToothMeaturesL1Model.h"          // 单次测量
#import "ByBlueToothMeaturesL2Model.h"          // 单次测量
#import "ByBlueToothMeaturesL3Model.h"          // 单次测量
#import "ByBlueToothMeaturesG1Model.h"          // 单次勾股
#import "ByBlueToothMeaturesG2Model.h"          // 双次勾股加
#import "ByBlueToothMeaturesG3Model.h"          // 双次勾股减
#import "ByBlueToothMeaturesS1Model.h"          // 长方形面积
#import "ByBlueToothMeaturesS2Model.h"          // 三角形面积
#import "ByBlueToothMeaturesS3Model.h"          // 梯形面积
#import "ByBlueToothMeaturesH1Model.h"          // 三角测量
#import "ByBlueToothMeaturesH2Model.h"          // 直角测量
#import "ByBlueToothMeaturesV1Model.h"          // 长方体体积
#import "ByBlueToothMeaturesX1Model.h"          // 距离+水平
#import "ByBlueToothMeaturesP1Model.h"          // 加法
#import "ByBlueToothMeaturesM1Model.h"          // 减法

@implementation ByBlueToothDataOperationModel

// 处理场景测量页面的距离显示用
+ (NSString *)displayDistanceOperation:(NSString *)dataHex {
    
    if (dataHex.length < 28) {
        return nil;
    }
    // 接收到的数据，如果不是单次测量的数据，则忽略
    if (![[dataHex substringWithRange:NSMakeRange(28, 4)] isEqualToString:@"4c31"] &&
        ![[dataHex substringWithRange:NSMakeRange(28, 4)] isEqualToString:@"4c33"]) {
        
        return nil;
    }
    
    NSString *dataStr = [dataHex substringWithRange:NSMakeRange(42, 18)];
    
    NSMutableString *retStr = [[NSMutableString alloc] init];
    for (int i = 0; i < dataStr.length; i += 2) {
        NSString *sub = [dataStr substringWithRange:NSMakeRange(i, 2)];
        char c1 = [sub characterAtIndex:0];
        char c2 = [sub characterAtIndex:1];
        NSString *subData = [NSString stringWithFormat:@"%c%c", c1, c2];
        
        // 16进制字符串转数字
        unsigned long long result = 0;
        NSScanner *scanner = [NSScanner scannerWithString:subData];
        [scanner scanHexLongLong:&result];
        
        // 拼接字符串
        [retStr appendFormat:@"%c", (char)result];
    }
    
    CGFloat number = [retStr floatValue];
    return [NSString stringWithFormat:@"%.3f", number];

}


// 实时测量
+ (NSNumber *)real_timeMeasureDataOperation:(NSString *)dataHex {
    
    // 接收到的传递产品信息的数据，则不做处理
    if ([[dataHex substringWithRange:NSMakeRange(28, 4)] isEqualToString:@"5648"]) {
        
        return nil;
    }
    
    
    NSString *dataStr = [dataHex substringWithRange:NSMakeRange(42, 18)];
    
    NSMutableString *retStr = [[NSMutableString alloc] init];
    for (int i = 0; i < dataStr.length; i += 2) {
        NSString *sub = [dataStr substringWithRange:NSMakeRange(i, 2)];
        char c1 = [sub characterAtIndex:0];
        char c2 = [sub characterAtIndex:1];
        NSString *subData = [NSString stringWithFormat:@"%c%c", c1, c2];

        // 16进制字符串转数字
        unsigned long long result = 0;
        NSScanner *scanner = [NSScanner scannerWithString:subData];
        [scanner scanHexLongLong:&result];
        
        // 拼接字符串
        [retStr appendFormat:@"%c", (char)result];
    }
    
    NSNumber *dataNumber = [NSNumber numberWithDouble:[retStr doubleValue]];
    return dataNumber;
}


// 处理历史数据
+ (NSDictionary *)historyDataOperation:(NSString *)dataHex {
    
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    // 取出多条数据（根据开头F5，结尾FA）
    NSArray *datas = [dataHex componentsSeparatedByString:@"f5"];
    
    for (NSString *dataStr in datas) {
        // 去掉F0那条应答数据，去掉空数据，一定要fa结尾
        if (![dataStr isEqualToString:@"f0"] && ![dataStr isEqualToString:@""] && [dataStr hasSuffix:@"fa"]) {
            [temp addObject:[NSString stringWithFormat:@"f5%@", dataStr]];
        }
       
    }
    
    NSMutableDictionary *retDic = [NSMutableDictionary dictionary];
    for (NSString *dataStrHex in temp) {
        NSString *typeStr =  [dataStrHex substringWithRange:NSMakeRange(28, 4)];
        ByBlueToothMeaturesModel *model = nil;
        if ([typeStr isEqualToString:@"5648"]) {    // 产品信息，忽略掉
            continue;
        }
        if ([typeStr isEqualToString:@"4c31"]) {    // 单次测量
            model = [[ByBlueToothMeaturesL1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"L1" model:model];
        }
        if ([typeStr isEqualToString:@"4c32"]) {    // 单次测量
            model = [[ByBlueToothMeaturesL2Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"L2" model:model];
        }
        if ([typeStr isEqualToString:@"4c33"]) {    // 单次测量
            model = [[ByBlueToothMeaturesL3Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"L3" model:model];
        }
        if ([typeStr isEqualToString:@"4731"]) {    // 单次勾股
            model = [[ByBlueToothMeaturesG1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"G1" model:model];

        }
        if ([typeStr isEqualToString:@"4732"]) {    // 双次勾股加
            model = [[ByBlueToothMeaturesG2Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"G2" model:model];

        }
        if ([typeStr isEqualToString:@"4733"]) {    // 双次勾股减
            model = [[ByBlueToothMeaturesG3Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"G3" model:model];

        }
        if ([typeStr isEqualToString:@"5331"]) {    // 长方形面积
            model = [[ByBlueToothMeaturesS1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"S1" model:model];

        }
        if ([typeStr isEqualToString:@"5332"]) {    // 三角形面积
            model = [[ByBlueToothMeaturesS2Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"S2" model:model];

        }
        if ([typeStr isEqualToString:@"5333"]) {    // 梯形面积
            model = [[ByBlueToothMeaturesS3Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"S3" model:model];

        }
        if ([typeStr isEqualToString:@"5631"]) {    // 长方体体积
            model = [[ByBlueToothMeaturesV1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"V1" model:model];
        }
        if ([typeStr isEqualToString:@"4831"]) {    // 直角测量
            model = [[ByBlueToothMeaturesH1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"H1" model:model];
        }
        if ([typeStr isEqualToString:@"4832"]) {    // 直角测量
            model = [[ByBlueToothMeaturesH2Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"H2" model:model];
        }
        if ([typeStr isEqualToString:@"5831"]) {    // 距离+水平
            model = [[ByBlueToothMeaturesX1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"X1" model:model];
        }
        if ([typeStr isEqualToString:@"5031"]) {    // 加法
            model = [[ByBlueToothMeaturesP1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"P1" model:model];
        }
        if ([typeStr isEqualToString:@"4d31"]) {    // 减肥
            model = [[ByBlueToothMeaturesM1Model alloc] initWithDataHexString:dataStrHex];
            [self setModelForDic:retDic key:@"M1" model:model];
        }
    }
    
    NSLog(@"数据字典%@", retDic);
    
    return [retDic copy];
}


+ (void)setModelForDic:(NSMutableDictionary *)retDic
                   key:(NSString *)key
                 model:(ByBlueToothMeaturesModel *)model {
    
    if (model) {
        if ([retDic.allKeys containsObject:key]) {
            NSMutableArray *temp = retDic[key];
            [temp addObject:model];
        } else {
            NSMutableArray *temp = [[NSMutableArray alloc] init];
            [temp addObject:model];
            [retDic setObject:temp forKey:key];
        }
    }
}

@end
